package com.weeho.servlet.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 获取客户端请求头信息
 * 客户端请求头：
 * 
 */
public class SystemInfo extends HttpServlet {
	 public void doGet(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
	        response.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器
	        //通过设置响应头控制浏览器以UTF-8的编码显示数据
	        response.setHeader("content-type", "text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        Enumeration<String> reqHeadInfos = request.getHeaderNames();//获取所有的请求头
	        out.write("获取到的客户端所有的请求头信息如下：");
	        out.write("<hr/>");
	        while (reqHeadInfos.hasMoreElements()) {
	            String headName = (String) reqHeadInfos.nextElement();
	            String headValue = request.getHeader(headName);//根据请求头的名字获取对应的请求头的值
	            out.write(headName+":"+headValue);
	            out.write("<br/>");
	        }
	        out.write("<br/>");
	        out.write("获取到的客户端Accept-Encoding请求头的值：");
	        out.write("<hr/>");
	        String value = request.getHeader("Accept-Encoding");//获取Accept-Encoding请求头对应的值
	        out.write(value);
	        
	        Enumeration<String> e = request.getHeaders("Accept-Encoding");
	        while (e.hasMoreElements()) {
	            String string = (String) e.nextElement();
	            System.out.println(string);
	        }
	        
	        String Agent = request.getHeader("User-Agent"); 
	        StringTokenizer st = new StringTokenizer(Agent,";"); 
	        st.nextToken(); 
	        //得到用户的浏览器名 
	        out.write("<br/>");
	        out.write("userbrowser："+getBrowserInfo(request));
	        //得到用户的操作系统名 
	        out.write("<br/>");
	        out.write("useros:"+getOsAndBrowserInfo(request));
	    }

	    public void doPost(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
	        doGet(request, response);
	    }
	    
	    /** 
	     * 获取操作系统,浏览器及浏览器版本信息 
	     * @param request 
	     * @return 
	     */  
	    protected  String getOsAndBrowserInfo(HttpServletRequest request){  
	        String  browserDetails  =   request.getHeader("User-Agent");  
	        String  userAgent       =   browserDetails;  
	  
	        String os = "";  
	  
	        //=================OS Info=======================  
	        if (userAgent.toLowerCase().indexOf("windows") >= 0 )  
	        {  
	            os = "Windows";  
	        } else if(userAgent.toLowerCase().indexOf("mac") >= 0)  
	        {  
	            os = "Mac";  
	        } else if(userAgent.toLowerCase().indexOf("x11") >= 0)  
	        {  
	            os = "Unix";  
	        } else if(userAgent.toLowerCase().indexOf("android") >= 0)  
	        {  
	            os = "Android";  
	        } else if(userAgent.toLowerCase().indexOf("iphone") >= 0)  
	        {  
	            os = "IPhone";  
	        }else{  
	            os = "UnKnown, More-Info: "+userAgent;  
	        }  
	        
	  
	        return os  ;  
	    }  
	    /**
	     * 获取浏览器信息
	     * @param request
	     * @return
	     */
	    protected  String getBrowserInfo(HttpServletRequest request){  
	        String  browserDetails  =   request.getHeader("User-Agent");  
	        String  userAgent       =   browserDetails;  
	        String  user            =   userAgent.toLowerCase();  
	  
	        String browser = "";  
	    	//===============Browser===========================  
	        if (user.contains("edge"))  
	        {  
	            browser=(userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");  
	        } else if (user.contains("msie"))  
	        {  
	            String substring=userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];  
	            browser=substring.split(" ")[0].replace("MSIE", "IE")+"-"+substring.split(" ")[1];  
	        } else if (user.contains("safari") && user.contains("version"))  
	        {  
	            browser=(userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]  
	                    + "-" +(userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];  
	        } else if ( user.contains("opr") || user.contains("opera"))  
	        {  
	            if(user.contains("opera")){  
	                browser=(userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]  
	                        +"-"+(userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];  
	            }else if(user.contains("opr")){  
	                browser=((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))  
	                        .replace("OPR", "Opera");  
	            }  
	  
	        } else if (user.contains("chrome"))  
	        {  
	            browser=(userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");  
	        } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1)  ||  
	                (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) ||  
	                (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1) )  
	        {  
	            browser = "Netscape-?";  
	  
	        } else if (user.contains("firefox"))  
	        {  
	            browser=(userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");  
	        } else if(user.contains("rv"))  
	        {  
	            String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");  
	            browser="IE" + IEVersion.substring(0,IEVersion.length() - 1);  
	        } else  
	        {  
	            browser = "UnKnown, More-Info: "+userAgent;  
	        }  
	        return browser;
	    }
}
